﻿using System.Text;
using System.Threading.Tasks;
using Model.Admin;
using Xproject.Framework.Utility.Extensions;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.ExceptionHandling;
using Xproject.Framework.Core;
using System.Linq.Expressions;
using Model.Admin.DTO;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Repository.WXProjectAdmin
{
    public static class AdminUserRep
    {
        public static AdminUser Get(this IRepositoryAsync<AdminUser> repository, string id)
        {
            Expression<Func<AdminUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }

        public static List<AdminUser> GetAllAdminUser(this IRepositoryAsync<AdminUser> repository,string site, string userName, int? userCode, string mobile, string email,
            DateTime? startTime, DateTime? endTime, string order, string ordername)
        {
            Expression<Func<AdminUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(userName))
            {
                express = express.AndAlso(i => i.UserName.Contains( userName));
            }
            if (userCode.HasValue)
            {
                express = express.AndAlso(i => i.UserCode==userCode);
            }
            if (!string.IsNullOrEmpty(mobile))
            {
                express = express.AndAlso(i => i.Mobile.Contains(mobile));
            }
            if (!string.IsNullOrEmpty(email))
            {
                express = express.AndAlso(i => i.Email.Contains(email));
            }
            if (startTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime >= startTime.Value);
            }
            if (endTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime <= endTime.Value);
            }        
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).ToList();
        }

        public static AdminUser GetByPassword(this IRepositoryAsync<AdminUser> repository, string userName, string password)
        {
            Expression<Func<AdminUser, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(userName))
            {
                express = express.AndAlso(i => i.UserName == userName);
            }
            if (!string.IsNullOrEmpty(password))
            {
                express = express.AndAlso(i => i.Password == password);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();

        }


    }
}
